use std::{sync::mpsc, thread::{sleep, spawn}, time::Duration};

fn main() {
    println!("Hello, world!");

    let (tx, rc) = mpsc::channel();

    let tx1 = tx.clone();
    spawn(move || {
        tx1.send("1").unwrap();
        sleep(Duration::from_secs(1));
        tx1.send("2").unwrap();
        sleep(Duration::from_secs(1));
        tx1.send("7").unwrap();
        sleep(Duration::from_secs(1));
        tx1.send("8").unwrap();
        sleep(Duration::from_secs(1));
    });

    spawn(move || {
        tx.send("3").unwrap();
        sleep(Duration::from_secs(1));
        tx.send("4").unwrap();
        sleep(Duration::from_secs(1));
        tx.send("5").unwrap();
        sleep(Duration::from_secs(1));
        tx.send("6").unwrap();
        sleep(Duration::from_secs(1));
    });

    // 只接收一次，可以这么写。当所有的发送端都关闭时就会返回一个错误
    // rc.recv().unwrap();
    // 接收多次
    for recstring in rc {
        println!("{}", recstring);
    }
}
